/*********************************************************************************
 **      Copyright:  (C) 2024 linuxer<linuxer@email.com>
 **                  All rights reserved.
 **
 **       Filename:  temperature.c
 **    Description:  This file 
 **
 **        Version:  1.0.0(17/03/24)
 **         Author:  linuxer <linuxer@email.com>
 **      ChangeLog:  1, Release initial version on "17/03/24 16:52:22"
 **                 
 **********************************************************************************/

#include "temperature.h"
#include <pthread.h>
#include <math.h>
#include <sys/syscall.h>
#include <unistd.h>

/* 功能：获取温度
 * 参数：一个ReportTemp 结构体的指针
 * 返回值	rv=0 表示获取成功 rv<0 表示获取失败*/
int get_temperature(float *temperature)
{
	int					rv=0;
	int 				fd=-1;
	char				file_name[64];
	char				buf[1024];
	char				*ptr=NULL;
	char				des[16]="28-";
	char				temp_path[64]="/sys/bus/w1/devices/";
	float				temp;
	pthread_t			tid;
	int					ret = 0;

	printf("temperature thread id = %d\n", syscall(SYS_gettid));

	pthread_mutex_init(&mut, NULL);
	pthread_create(&tid, NULL, threadPro, NULL);
	
	ret = pthread_mutex_lock(&mut);
	printf("1. ret = %d, lock = %d, count = %d, owner = %d", ret, mut.__data.__lock, mut.__data.__count, mut.__data.__owner);
	
	rv=find_file(temp_path, des, file_name);
	
	if (rv < 0)
	{
		log_error("Find file  failure: %s\n", strerror(errno));
		return -1;
	}

#if 1 
	strncat(temp_path, file_name, sizeof(temp_path)-strlen(temp_path));
	strncat(temp_path, "/w1_slave", sizeof(temp_path)-strlen(temp_path));

	if ((fd = open(temp_path,O_RDONLY)) < 0)
	{
		log_error("Open directory: %s failure: %s\n", temp_path,strerror(errno));
		return -2;
	}

	memset(buf,0,sizeof(buf));
	read(fd,buf,sizeof(buf));
	ptr=strstr(buf,"t=");

	if (!ptr)
	{
		log_error("Can not find the temperature\n");
		return -3;
	}

	ptr += 2;
	*temperature = roundf((atof(ptr) / 1000) * 100.0) / 100.0;
	close(fd);

	pthread_mutex_unlock(&mut);
	printf("2. ret = %d, lock = %d, count = %d, owner = %d", ret, mut.__data.__lock, mut.__data.__count, mut.__data.__owner);
	
	pthread_join(tid, NULL);
	pthread_mutex_destroy(&mut);

#endif 
	return rv;
}


/*
 * 功能：	通过文件名称和路径找到文件
 * 参数：	路径 文件完整路径 文件名称
 * 返回值：	rv=0 寻找文件成功 rv<0寻找文件失败
 */
int find_file( char *path, char *des, char *file_name)
{	
	DIR				*dirp=NULL;
	struct	dirent	*direntp=NULL;
	int				found=0;
	int				rv=0;

	dirp = opendir(path);
	if (!dirp)
	{
		log_error("Open the %s directory failure: %s", path, strerror(errno));
		rv = -1;
		goto cleanup;
	}

	while (NULL != (direntp = readdir(dirp)))
	{
		if (strstr(direntp->d_name, des))
		{
			strncpy(file_name, direntp->d_name, 64);
			found = 1;
		}
	}
	

	if (!found)
	{
		log_error("Find temperature failure: %s\n", strerror(errno));
		rv = -2;
		goto cleanup;
	}

	return rv;

cleanup:
	closedir(dirp);
	return rv;

}

void *threadPro(void *arg)
{
	printf("child thread id = %d, wait to get mutex....\n", syscall(SYS_gettid));
	int ret = pthread_mutex_lock(&mut);
	printf("in child thread, ret = %d, lock = %d, count = %d, owner = %d\n", ret, mut.__data.__lock, mut.__data.__count, mut.__data.__owner);
}
